<script lang="ts" setup>
import { ref, onMounted, reactive } from 'vue';
import { ElMessage, ElLoading } from 'element-plus';

// 是否显示加载中
const loading = ref(true);
// 聊天历史记录标志
const showHistory = ref(false);
// 聊天记录数据
const chatHistory = reactive([]);

// 控制参数
const settings = reactive({
  theme: 'light',
  fullscreen: false,
  showWelcomeScreen: true
});

// iframe引用
const chatFrame = ref(null);

// 加载完成处理
const handleIframeLoad = () => {
  loading.value = false;
  ElMessage.success('学习分析助手已准备就绪');
};

// 模拟获取聊天历史
const fetchChatHistory = () => {
  // 这里可以接入dify的GET /messages API获取真实历史
  const loadingInstance = ElLoading.service({
    target: '.history-panel',
    text: '加载聊天记录中...'
  });

  setTimeout(() => {
    chatHistory.push(
      { id: 1, message: "课程学习进度如何？", time: "2023-06-15 14:30" },
      { id: 2, message: "我的SQL查询有错误，能帮我分析吗？", time: "2023-06-14 10:20" },
      { id: 3, message: "JavaScript闭包概念解析", time: "2023-06-13 16:45" }
    );
    loadingInstance.close();
  }, 800);
};

// 切换全屏模式
const toggleFullscreen = () => {
  settings.fullscreen = !settings.fullscreen;
};

onMounted(() => {
  // 这里可以添加初始化代码
  // 如果需要使用dify API进行额外控制，可在此处实现
});
</script>

<template>
    <div class="study-analyse-container" :class="{ 'fullscreen': settings.fullscreen }">
    <!-- 标题栏 -->
    <div class="header">
      <h2>学习分析智能助手</h2>
      <div class="controls">
        <el-tooltip content="查看历史记录" placement="top">
          <el-button
            @click="showHistory = !showHistory; if(showHistory && !chatHistory.length) fetchChatHistory()"
            circle
            :icon="showHistory ? 'Close' : 'Menu'"
            size="small"
          ></el-button>
        </el-tooltip>
        <el-tooltip :content="settings.fullscreen ? '退出全屏' : '全屏模式'" placement="top">
          <el-button
            @click="toggleFullscreen"
            circle
            :icon="settings.fullscreen ? 'Close' : 'FullScreen'"
            size="small"
          ></el-button>
        </el-tooltip>
        <el-dropdown trigger="click">
          <el-button circle icon="Setting" size="small"></el-button>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>
                <el-switch
                  v-model="settings.theme"
                  inline-prompt
                  active-text="浅色"
                  inactive-text="深色"
                  active-value="light"
                  inactive-value="dark"
                  style="width: 100%"
                />
              </el-dropdown-item>
              <el-dropdown-item>
                <el-checkbox v-model="settings.showWelcomeScreen">显示欢迎界面</el-checkbox>
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>

    <!-- 主体内容 -->
    <div class="main-content">
      <!-- 聊天历史侧边栏 -->
      <div class="history-panel" v-if="showHistory">
        <h3>历史会话</h3>
        <el-empty v-if="!chatHistory.length" description="暂无历史记录"></el-empty>
        <el-scrollbar height="100%" v-else>
          <div
            v-for="item in chatHistory"
            :key="item.id"
            class="history-item"
          >
            <div class="history-message">{{ item.message }}</div>
            <div class="history-time">{{ item.time }}</div>
          </div>
        </el-scrollbar>
      </div>

      <!-- Dify智能体iframe -->
      <div class="chat-container" :class="{'with-history': showHistory}">
        <div v-show="loading" class="loading-overlay">
          <el-spinner type="dots" />
          <p>智能助手加载中...</p>
        </div>

        <iframe
          ref="chatFrame"
          src="http://121.40.242.43/chatbot/XBk4mLhwPsK9QW7k"
          frameborder="0"
          allow="microphone"
          @load="handleIframeLoad"
        ></iframe>

        <div class="tips-panel">
          <p><i class="el-icon-info"></i> 您可以随时向AI助手提问关于学习分析和课程内容的问题</p>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.study-analyse-container {
  display: flex;
  flex-direction: column;
  height: 100%;
  background: var(--el-bg-color);
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  overflow: hidden;
  transition: all 0.3s ease;
}

.fullscreen {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: 9999;
  border-radius: 0;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 20px;
  border-bottom: 1px solid var(--el-border-color-light);
  background: linear-gradient(135deg, #1C64F2 0%, #5A94FF 100%);
  color: #fff;
}

.header h2 {
  margin: 0;
  font-weight: 500;
  font-size: 18px;
}

.controls {
  display: flex;
  gap: 8px;
}

.main-content {
  display: flex;
  flex: 1;
  overflow: hidden;
  position: relative;
}

.history-panel {
  width: 240px;
  border-right: 1px solid var(--el-border-color-light);
  padding: 16px;
  background-color: var(--el-bg-color-page);
  display: flex;
  flex-direction: column;
}

.history-panel h3 {
  margin-top: 0;
  margin-bottom: 16px;
  font-size: 16px;
  color: var(--el-text-color-primary);
}

.history-item {
  padding: 12px;
  border-radius: 6px;
  margin-bottom: 10px;
  background-color: var(--el-bg-color);
  cursor: pointer;
  transition: background-color 0.2s;
}

.history-item:hover {
  background-color: var(--el-color-primary-light-9);
}

.history-message {
  font-size: 14px;
  color: var(--el-text-color-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history-time {
  font-size: 12px;
  color: var(--el-text-color-secondary);
  margin-top: 4px;
}

.chat-container {
  flex: 1;
  position: relative;
  display: flex;
  flex-direction: column;
  transition: all 0.3s ease;
}

.chat-container.with-history {
  margin-left: 0;
}

.loading-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(255, 255, 255, 0.8);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: 10;
}

iframe {
  flex: 1;
  width: 100%;
  height: calc(100% - 40px);
  min-height: 600px;
  border: none;
}

.tips-panel {
  height: 40px;
  padding: 0 16px;
  display: flex;
  align-items: center;
  background-color: var(--el-color-info-light-9);
  color: var(--el-text-color-secondary);
  font-size: 13px;
}

/* 响应式设计 */
@media screen and (max-width: 768px) {
  .history-panel {
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    z-index: 5;
  }

  .chat-container.with-history {
    margin-left: 240px;
  }
}
</style>